<?php
namespace app\admin\controller;
use think\facade\Db;
use app\admin\model\Customer as CustomerModel;
use app\admin\model\MediaId;
use app\common\BaseController;
use hg\apidoc\annotation as Apidoc;
use think\Request;
use think\exception\ValidateException;
use app\common\support\PHPExcelService;

class Customer extends BaseController{
    

    public function index(Request $request){
        $page  = (int)$request->param('page/d', 0);
        $limit  = (int)$request->param('limit/d', 10);
        $search = request()->param();
        $where = [];
        if(isset($search["keyword"]) && $search["keyword"]!="") $where[] = ["keyword","=",$search["keyword"]];
        if(isset($search["content"]) && $search["content"]!="") $where[] = ["content","=",$search["content"]];
        $query  = CustomerModel::where($where);
        $total  = $query->count();
        $data   = $query->page($page,$limit)->order('id desc')->select();

        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
        ]);
    }

    

    public function detail(int $id): \think\response\Json
    {
        try {
            $row  = CustomerModel::where('id', $id)->find();
            if($row->msg_type == 2){
                $row->media_url = MediaId::where('media_id', $row->media_id)->value('url');
            }elseif($row->msg_type == 4){
                $row->media_url = MediaId::where('media_id', $row->thumb_media_id)->value('url');
            }
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return $this->error($e->getMessage());
        }

        return $this->success('', $row);
    }

 
       public function create(Request $request): \think\response\Json
    {

        $data = $request->post();
        if($data['thumbUrl']){
            $data['thumb_url'] = $data['thumbUrl'];
        }
        try {
            $this->validate($data, [
                'keyword' => 'require',
                'msg_type' => 'require',
            ], [
               'keyword.require'  => '关键字不得为空',
               'msg_type.require'  => '消息类型不得为空',
            ]);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
       
        $result = CustomerModel::create($data);
        if ($result->isEmpty()) {
            return $this->error('错误');
        }

        return $this->success('成功');
    }



 
    public function edit(){
        $data = request()->post();
         try {
            $this->validate($data, [
                'id' => 'require',
            ], [
                'id.require'  => '主键必须'
            ]);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        if(CustomerModel::update($data,['id'=>$data['id']])) return $this->success('操作成功');
        return $this->error('操作失败');
    }





        /**
     * @Apidoc\Title ("删除")
     * @Apidoc\Url("Customer_delete")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", require=true, desc="id")
     */
    public function delete(): \think\response\Json
    {
        $ids = request()->post('ids/s');
        $ids = explode(',', $ids);
        $row = CustomerModel::where('id','in', $ids)->select();
        if (!$row) {
            return $this->error('错误');
        }
        $row->delete();
        return $this->success('成功');
    }



}